package com.qf.controller;

import com.qf.service.DtsGoodsProductService;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsOrderService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/admin/dashboard")
public class DtsAdminController {
    @Autowired
    DtsUserService userService;
    @Autowired
    DtsGoodsService goodsService;
    @Autowired
    DtsGoodsProductService productService;
    @Autowired
    DtsOrderService orderService;
    @GetMapping("/chart")
    public Object chart(){
        //查询 分类和分类的销售额
        List<Map<String,Object>> categoryShellData = orderService.findCategorySellData();
        List<Object> categoryNames = categoryShellData.stream().map((map) -> {
            return map.get("name");
        }).collect(Collectors.toList());
        Map<String,Object > categorySell = new HashMap<>();
        categorySell.put("categoryNames", categoryNames);
        categorySell.put("categorySellData",  categoryShellData);
        //查询 （最近一个月） 日期和对应的订单总额， 订单笔数
        //dayStatis ： 按天 查询  订单总额，订单笔数
        List<DayStatis> orderDayStatis = orderService.findOrderAmts();

        //提取日期
        List<String> orderDayData = fetchDayData(orderDayStatis);

        List<BigDecimal> orderAmtData = new ArrayList<>();
        List<Integer> orderCntData = new ArrayList<>();
        //把数据提取，提取订单的总额
        for (String day : orderDayData) {
            for (DayStatis dayStatis : orderDayStatis) {
                if(dayStatis.getDayStr().equals(day)) {
                    orderAmtData.add(dayStatis.getAmts());
                    orderCntData.add(dayStatis.getCnts());
                    break;
                }
            }
        }
        //封装 订单的日期，订单的总额，订单的笔数
        Map<String,Object> orderAmts = new HashMap<>();
        orderAmts.put("dayData",  orderDayData);
        orderAmts.put("orderAmtData",  orderAmtData);
        orderAmts.put("orderCntData",  orderCntData);
        List<DayStatis> userDayStatis = userService.findUserDayStatis();
        //整合数据
        List<String> userDayData = fetchDayData(userDayStatis);
        //合并两个日期集合（保证升序，且不重复）
        List<String> oderUserDayData = unionDayList(orderDayData, userDayData);
        //提取 用户的数量， 订单的数量
        List<Integer> userCnt = new ArrayList<>();
        List<Integer> orderCnt = new ArrayList<>();
        //遍历合并的日期集合,找到合适的dayStatis，提取数量
        a:for (String day : oderUserDayData) {
            //遍历用户的 数据
            for (DayStatis dayStatis : userDayStatis) {
                //如果日期匹配，添加到list集合中
                if(day.equals(dayStatis.getDayStr())){
                    userCnt.add(dayStatis.getCnts());
                    continue a;//继续循环
                }
            }
            //内循环，循环完成后 ，依然没有找到合适的日期
            userCnt.add(0);
        }

        a:for (String day : oderUserDayData) {
            //遍历用户的 数据
            for (DayStatis dayStatis : orderDayStatis) {
                //如果日期匹配，添加到list集合中
                if(day.equals(dayStatis.getDayStr())){
                    orderCnt.add(dayStatis.getCnts());
                    continue a;
                }
            }
            //内循环，循环完成后 ，依然没有找到合适的日期
            orderCnt.add(0);
        }
        //整合 用户和订单的数据
        Map<String,Object> userOrderCnt = new HashMap<>();
        userOrderCnt.put("dayData", oderUserDayData);
        userOrderCnt.put("userCnt", userCnt);
        userOrderCnt.put("orderCnt", orderCnt);
        //封装结果
        Map<String, Object> data = new HashMap<>();
        data.put("categorySell",categorySell );
        data.put("orderAmts",orderAmts );
        data.put("userOrderCnt",userOrderCnt );
        return ResponseUtil.ok(data);
    }

    private List<String> unionDayList(List<String> orderDayData, List<String> userDayData) {
        //特点：不可以重复, 无序的
        Set<String> set = new HashSet<>();
        set.addAll(orderDayData);
        set.addAll(userDayData);
        //转换为list集合
        List<String> list = new ArrayList<>();
        list.addAll(set);
        //升序排序
        Collections.sort(list);
        return list;
    }
    private List<String> fetchDayData(List<DayStatis> dayStatis) {
        List<String> dayStrList = dayStatis.stream().map((statis) -> {
            return statis.getDayStr();
        }).collect(Collectors.toList());
        //升序排序
        Collections.sort(dayStrList);
        return dayStrList;
    }
    @GetMapping("")
    public Object info(){
        int userTotal = userService.getTotal();
        int goodsTotal = goodsService.getTotal();
        int productTotal = productService.getTotal();
        int orderTotal = orderService.getTotal();

        Map<String,Object> data = new HashMap<>();
        data.put("userTotal", userTotal);
        data.put("goodsTotal", goodsTotal);
        data.put("productTotal", productTotal);
        data.put("orderTotal", orderTotal);
        return ResponseUtil.ok(data);

    }
}